package main

import (
	"github.com/streadway/amqp"
	"log"
	"os"
	"strings"
)

func bodyform(args []string) string {
	var s string
	if (len(args) < 2) || args[1] == "" {
		s = "hello"
	} else {
		s = strings.Join(args[1:], " ")
	}
	return s
}
func severityFrom(args []string) string {
	var s string
	if (len(args) < 2) || os.Args[1] == "" {
		s = "info"
	} else {
		s = os.Args[1]
	}
	return s
}
func main() {
	//connect
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	if err != nil {
	}
	defer conn.Close()

	//channels
	channel, err := conn.Channel()
	if err != nil {
	}
	defer channel.Close()
	// exchange
	channel.ExchangeDeclare(
		"logs_direct",
		"direct",
		true,
		false,
		false,
		false,
		nil,
	)
	body := bodyform(os.Args)
	channel.Publish(
		"logs_direct",
		severityFrom(os.Args),
		false,
		false,
		amqp.Publishing{ContentType: "text/plain", Body: []byte(body)},
	)
	log.Printf("Sent %s\n", body)
}
